package org.linlinjava.litemall.admin.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.service.AdvertisingService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.Response;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/admin/ad")
public class AdminAdController {

    @Autowired
    AdvertisingService advertisingService;

    @GetMapping("/list")
    public Object list(String name, String content,
                       @RequestParam(defaultValue = "1") Integer page, Integer limit,
                       @RequestParam(defaultValue = "desc") String order){
        return ResponseUtil.okList(advertisingService.getAdPage(name,content,page,limit,order));
    }

    @PostMapping("/delete")
    public Object del(@RequestBody LitemallAd ad){
        if(ad == null){
            ResponseUtil.badArgument();
        }
        advertisingService.delete(ad);
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object modify(@RequestBody LitemallAd ad){
        ad.setUpdateTime(new Date());
        boolean modify = advertisingService.modify(ad);
        if(modify){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/create")
    public Object create(@RequestBody LitemallAd ad) throws IOException {
        ad.setDeleted(false);
        ad.setAddTime(new Date());
        boolean add = advertisingService.add(ad);
        if(add){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

//    @GetMapping("/readAd")
//    public Object readAd(Integer id){
//        advertisingService.modify(ad)
//        return null;
//    }


}
